package com.b2c.oa.controller;

import com.b2c.common.third.thymeleaf.PagingResponse;
import com.b2c.oa.DataConfigObject;
import com.ims.entity.ManageUserEntity;
import com.ims.service.ManageUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class SysSettingController {
    @Autowired
    private ManageUserService userService;

    /**用户列表**/
    @RequestMapping(value = "/tdgl/user_list",method = RequestMethod.GET)
    public String uerList(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");
        if (Integer.parseInt(userId.toString())!=1) return "/permission_denied";

        Integer pageIndex = 1;
        Integer pageSize = DataConfigObject.getInstance().getPageSize();
        String page = request.getParameter("page");
        if (!StringUtils.isEmpty(page)) {
            pageIndex = Integer.parseInt(page);
        }
        String name = "";
        if (!StringUtils.isEmpty(request.getParameter("name"))) {
            name = request.getParameter("name");
        }
        String mobile = "";
        if (!StringUtils.isEmpty(request.getParameter("mobile"))) {
            mobile = request.getParameter("mobile");
        }



        PagingResponse<ManageUserEntity> result = userService.getUserList(pageIndex, pageSize, name, mobile);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());
        model.addAttribute("mUserId",userId);

        return "sys_tdgl";
    }
    /**
     * 设置用户权限
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/tdgl/sys_cdsd",method = RequestMethod.GET)
    public String sysCdsd(Model model, HttpServletRequest request){
        Integer sUserId=Integer.parseInt(request.getParameter("userId"));
        model.addAttribute("lists", userService.getUserPerMissionMenu(sUserId));
        model.addAttribute("userId",sUserId);
        return "sys_cdsd";
    }
    /**
     * 设置用户菜单权限
     * @param request
     * @return
     */
    @RequestMapping(value = "/tdgl/set_user_menu", method = RequestMethod.POST)
    public String setManageUserMenu(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getParameter("user_id"));
        userService.setUserMenu(userId,request.getParameterValues("purview"));
        return "redirect:/tdgl/user_list";
    }


}
